package com.bigmeal.common.pdf;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by dongfangchao on 2017/2/28/0028.
 */
public class ToImageUtils {

    public static final String TEMP_FILE_PREFIX = "tmpFile";

    public static File pdfToImg(String filePath, Integer pageNo, String imgType) throws Exception{
        try {
            File tmpFile = getTmpFile(imgType);
            PDDocument doc = getPdfDoc(new File(filePath));
            createImg(doc, pageNo, tmpFile, imgType);
            return tmpFile;
        } catch (IOException e) {
            throw new Exception("图片转换失败");
        }
    }

    public static File pdfToImgLastPage(String filePath, String imgType) throws Exception{
        return pdfToImg(filePath, ImageConstants.LAST_PAGE, imgType);
    }

    public static File pdfToImgFirstPage(String filePath, String imgType) throws Exception{
        return pdfToImg(filePath, ImageConstants.FIRST_PAGE, imgType);
    }

    public static File pdfToImg(byte[] fileBytes, Integer pageNo, String imgType) throws Exception{
        try {
            File tmpFile = getTmpFile(imgType);
            PDDocument doc = getPdfDoc(fileBytes);
            createImg(doc, pageNo, tmpFile, imgType);
            return tmpFile;
        } catch (IOException e) {
            throw new Exception("图片转换失败");
        }
    }

    public static File pdfToImgLastPage(byte[] fileBytes, String imgType) throws Exception{
        return pdfToImg(fileBytes, ImageConstants.LAST_PAGE, imgType);
    }

    public static File pdfToImgFirstPage(byte[] fileBytes, String imgType) throws Exception{
        return pdfToImg(fileBytes, ImageConstants.FIRST_PAGE, imgType);
    }

    private static PDDocument getPdfDoc(byte[] fileBytes) throws Exception{
        return PDDocument.load(fileBytes);
    }

    private static PDDocument getPdfDoc(File file) throws Exception{
        return PDDocument.load(file);
    }

    private static void createImg(PDDocument doc, Integer pageNo, File tmpFile, String imgType) throws Exception{
        PDFRenderer renderer = new PDFRenderer(doc);
        int pageCount = doc.getNumberOfPages();
        if (pageNo == ImageConstants.LAST_PAGE){
            pageNo = pageCount;
        }else if (pageCount < pageNo || pageNo <= 0){
            throw new Exception("指定页超出pdf页数");
        }
        BufferedImage image = renderer.renderImageWithDPI(pageNo - 1, 96);
        ImageIO.write(image, imgType, tmpFile);
    }

    private static File getTmpFile(String imgType) throws Exception{
        File tmpFile = File.createTempFile(TEMP_FILE_PREFIX, "." + imgType);
        tmpFile.deleteOnExit();
        return tmpFile;
    }

    class ImageConstants{
        public static final String IMG_TYPE_JPG = "jpg";
        public static final String IMG_TYPE_PNG = "png";
        public static final int LAST_PAGE = -1;
        public static final int FIRST_PAGE = 1;
    }

}
